<template>
  <div v-loading="loading">
    <tableComponents
      :pagination="{currentPage,pageSize,total:list.all_num}"
      @handleCurrentChange="handleCurrentChange"
      @handleSizeChange="handleSizeChange"
      @sendUserSearch="getSearch"
      :searchData="userSearchData"
      :isHaveExport="false"
      @toAdd="updateNews(0)"
    >
      <template v-slot:table>
        <el-table
          element-loading-text="拼命加载中"
          ref="multipleTable"
          :data="list.data"
          stripe
          header-row-class-name="head_color"
          tooltip-effect="dark"
          style="width: 99.8%"
          @selection-change="handleSelectionChange"
        >
          <el-table-column type="selection" width="35"></el-table-column>

          <el-table-column label="图片" min-width="80">
            <template slot-scope="scope">
              <el-image :src="scope.row.piclink" class="sucai">
                <div slot="error" class="image-slot">
                  <img src="../../../assets/img/productDefault.png" alt />
                </div>
              </el-image>
            </template>
          </el-table-column>

          <el-table-column label="标题" min-width="160">
            <template slot-scope="scope">
              <div class="user_table">
                <span>房产标题：</span>
                <small>{{scope.row.title}}</small>
                <br />
                <span>房产标签：</span>
                <small>{{scope.row.sub_title}}</small>
                <p>
                  <span>开发商：</span>
                  <small>{{scope.row.developer}}</small>
                </p>
                <p>
                  <span>物业公司：</span>
                  <small>{{scope.row.property_company}}</small>
                </p>

                <p>
                  <span>售楼处：</span>
                  <small>{{scope.row.sales_office}}</small>
                </p>
              </div>
            </template>
          </el-table-column>

          <el-table-column label="面积" min-width="160">
            <template slot-scope="scope">
              <div class="user_table">
                <span>最低成交价格：</span>
                <small>{{scope.row.min_price+'万' || '----'}}</small>
                <br />
                <span>最高成交价格：</span>
                <small>{{scope.row.max_price+'万' || '----'}}</small>
                <br />
                <span>占地面积：</span>
                <small>{{scope.row.floor_area+'m²' || '----'}}</small>
                <br />
                <span>建筑面积：</span>
                <small>{{scope.row.construction_area+'m²' || '----'}}</small>
                <br />
                <span>绿化率：</span>
                <small>{{scope.row.greening_rate+'%' || '----'}}</small>
              </div>
            </template>
          </el-table-column>

          <el-table-column label="信息" min-width="160">
            <template slot-scope="scope">
              <div class="user_table">
                <span>物业特色：</span>
                <small>{{scope.row.property_features}}</small>
                <br />
                <span>主力户型：</span>
                <small>{{scope.row.main_unit}}</small>
                <br />
                <span>房屋类型：</span>
                <small>{{scope.row.property_type}}</small>
                <br />
                <span>装修情况：</span>
                <small>{{scope.row.renovation_condition}}</small>
                <br />
                <span>建筑类型：</span>
                <small>{{scope.row.building_type}}</small>
              </div>
            </template>
          </el-table-column>

          <el-table-column label="房屋" min-width="160">
            <template slot-scope="scope">
              <div class="user_table">
                <span>产权年限：</span>
                <small>{{scope.row.property_age}}</small>
                <br />
                <span>交房时间：</span>
                <small>{{scope.row.expected_delivery | getTimeFormat2}}</small>
                <br />
                <span>车位数：</span>
                <small>{{scope.row.number_of_cars}}</small>
                <br />
                <span>规划户数：</span>
                <small>{{scope.row.planning_households}}</small>
                <br />
                <span>物业费：</span>
                <small>{{scope.row.property_costs}}/元m²</small>
              </div>
            </template>
          </el-table-column>

          <el-table-column label="是否上架" min-width="130" show-overflow-tooltip>
            <template slot-scope="scope">
              <el-switch
                class="zdySwith"
                :width="82"
                v-model="scope.row.show"
                active-color="#13ce66"
                inactive-color="#ffffff"
                active-text="上架"
                inactive-text="下架"
                active-value="1"
                @change="getIskeck($event,scope.row)"
                inactive-value="0"
              ></el-switch>
            </template>
          </el-table-column>

          <el-table-column label="操作" min-width="150">
            <template slot-scope="scope">
              <el-button
                :loading="$store.state.isLoading"
                size="mini"
                type="primary"
                @click="updateNews(scope.row.id)"
              >编辑</el-button>
              <el-button
                :loading="$store.state.isLoading"
                @click="copyproduct(scope.row.id)"
                size="mini"
              >复制</el-button>
              <el-button
                :loading="$store.state.isLoading"
                size="mini"
                type="danger"
                @click="deleteItems( scope.row.id)"
              >删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </template>
      <template v-slot:footerLeft>
        <a class="aBtn aBtn_1 aBtn_lan2" @click="deleteLises">删除</a>
      </template>
    </tableComponents>

    <el-dialog
      :close-on-click-modal="false"
      title="复制"
      :modal-append-to-body="false"
      :visible.sync="copy_show"
      class="tl size_13"
      width="800px"
    >
      <el-form ref="data" class="mar_top" label-width="150px">
        <el-form-item label="房产分类选择：">
          <el-cascader
            :options="cate_option"
            :props="optionProps"
            :show-all-levels="false"
            v-model="copy_form.cate_id"
            size="small"
            change-on-select
            placeholder="房产分类"
            clearable
          ></el-cascader>
        </el-form-item>
        <div class="mar_bottom_15">
          <span class="sizeColor size_13 sp_style"></span>
          <el-button
            :loading="$store.state.isLoading"
            size="medium"
            type="primary"
            @click.prevent="copy_shop_btn()"
          >复制</el-button>
        </div>
      </el-form>
    </el-dialog>
  </div>
</template>
<script>
import { estateSearchData } from "@/assets/js/searchData/searchData"; //引入搜索的数据
export default {
  data() {
    return {
      userSearchData: estateSearchData,
      list: "",
      pageSize: 10,
      currentPage: 1,
      dialogFormVisible: false,
      dioLoading: false,
      uddateIndex: "",
      multipleSelection: [],
      updateId: "",
      loading: false,
      showImg: true,
      searchAll: {},
      prevList: null,
      bool: true,
      cate_option: [],
      copy_form: {
        id: "",
        cate_id: ""
      },
      optionProps: {
        value: "id",
        label: "title",
        children: "z"
      },
      copy_show: false
    };
  },
  methods: {
    getIskeck(val, row) {
      var params = {};
      params.id_str = row.id;
      params.show_type = val;

      this.$api.getEstate.checkShowEstate(params).then(res => {
        // eslint-disable-next-line no-empty
        if (res.data.code == 200) {
        } else {
          this.$swal("操作失败！", "出错啦！", "success");
        }
      });
    },

    copy_shop_btn() {
      var params = {};
      params.id = this.copy_form.id;
      params.cate_id = this.copy_form.cate_id[0];
      this.$api.getEstate.copyEstate(params).then(res => {
        if (res.data.code == 200) {
          this.$fnc.alertSuccess({ text: "复制成功" });
          var paramsss = this.searchAll;
          paramsss.page = "";
          paramsss.page_size = this.pageSize;
          this.getNewsAll(paramsss);
          this.copy_show = false;
        } else {
          this.$fnc.alertError(res.data.code_desc, res.data.result);
        }
      });
    },
    copyproduct(id) {
      var params = {};
      params.parent_id = "";
      this.$api.getEstate.getEstateCateTreeList(params).then(res => {
        if (res.data.code == 200 && res.data.status == 1 && res.data.result) {
          this.cate_option = res.data.result;
        }
      });
      this.copy_form.id = id;
      this.copy_form.cate_id = "";
      this.copy_show = true;
    },
    updateNews(id) {
      var routeData;
      console.log(id);
      if (id) {
        routeData = this.$router.resolve({
          path: "/index/estate/add",
          query: { id: id }
        });
      } else {
        routeData = this.$router.resolve({
          path: "/index/estate/add"
        });
      }

      window.open(routeData.href, "_blank");
    },
    getSearch(obj) {
      this.loading = true;
      this.searchAll = obj;

      this.$router.push(this.$route.path + "?page=" + 1);

      this.getNewsAll(obj);
    },
    getNewsAll(data) {
      this.loading = true;
      var params = {};
      data = data || {};
      params = data;
      for (var i in params) {
        if (params[i] == undefined) {
          params[i] = "";
        }
      }
      this.$api.getEstate.getEstateLists(params).then(res => {
        if (res.data.code == 200 && res.data.status == 1) {
          this.list = res.data.result;
        } else {
          this.$fnc.alertError(res.data.code_desc, res.data.result);
        }
        this.loading = false;
      });
    },
    handleSizeChange(val) {
      var params = this.searchAll;
      params.page = "";
      params.page_size = val;

      this.getNewsAll(params);
      this.pageSize = val;
      // console.log(`每页 ${val} 条`);
    },
    handleCurrentChange(val) {
      this.$router.push(this.$route.path + "?page=" + val);
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    deleteItems(id) {
      this.$swal({
        title: "您确定要删除这条信息吗",
        text: "删除后将无法恢复，请谨慎操作！",
        type: "warning",
        showCancelButton: true,
        confirmButtonColor: "#DD6B55",
        confirmButtonText: "是的，我要删除",
        cancelButtonText: "容我三思"
      }).then(willDelete => {
        if (willDelete.value) {
          var data = { id };
          this.$api.getEstate.delEstate(data).then(res => {
            if (res.data.code == 200) {
              this.$swal("删除成功！", "您已经删除了这条信息。", "success");
              this.getNewsAll(this.searchAll);
            } else {
              this.$swal("删除失败！", "出错啦！", "success");
            }
          });
        }
      });
    },

    deleteLises() {
      //批量删除
      var str = "";
      if (this.multipleSelection.length <= 0) {
        this.$message.error("未选择批量删除");
      } else {
        for (var i in this.multipleSelection) {
          str += this.multipleSelection[i].id + "@";
        }
        str = str.slice(0, -1);
        this.$swal({
          title: "您确定要删除这些信息吗",
          text: "删除后将无法恢复，请谨慎操作！",
          type: "warning",
          showCancelButton: true,
          confirmButtonColor: "#DD6B55",
          confirmButtonText: "是的，我要删除",
          cancelButtonText: "容我三思"
        }).then(willDelete => {
          if (willDelete.value) {
            var data = { id_str: str };
            this.$api.getEstate.delAllEstate(data).then(res => {
              if (res.data.code == 200) {
                this.$swal("删除成功！", "您已经删除了这些信息。", "success");
                this.getNewsAll(this.searchAll);
              } else {
                this.$swal("删除失败！", "出错啦！", "success");
              }
            });
          }
        });
      }
    }
  },

  watch: {
    $route() {
      var params = this.searchAll;
      params.page = this.$route.query.page || 1;
      params.page_size = this.pageSize;
      this.getNewsAll(params);
      this.currentPage = Number(this.$route.query.page) || 1;
    }
  },
  beforeCreate() {},
  created() {
    this.getNewsAll();
  }
};
</script>
